// Copyright  2012 Petr TALLA. [petr.talla@gmail.com]
// This file is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This file is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY;  without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
// [http://www.gnu.org/copyleft/lesser.html]
//=============================================================================

package tc.crase.meta;

import java.util.Vector;

import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.node.ArrayNode;
import org.codehaus.jackson.node.ObjectNode;

import tc.crase.db.Xattr;
import tc.crase.sys.XobjectEx;

public class Type {
	public String id = new String("");
	
	public Vector<Attrdef> attrdefs = new Vector<Attrdef>(); 
	public Vector<Descr> descrs = new Vector<Descr>();
	
	public Type(String id) {
		this.id = new String(id);
	}
	
	public void initialize(XobjectEx obj) {
		for (int i = 0; i < attrdefs.size(); i++ ) {
			Attrdef attrdef = attrdefs.get(i);
			attrdef.initialize(obj);
		}
	}
	
	public String getObjectValue(XobjectEx xobj) {
		for (int i = 0; i < descrs.size(); i++ ) {
			String result = descrs.get(i).getValue(xobj);
			if (result.isEmpty()) continue;
			return result;
		}
		
		return "";
	}
	
	public void save(ObjectNode jsonType, ObjectMapper mapper) {
		jsonType.put("id", id);
		
		ArrayNode jsonAttrdefs = mapper.createArrayNode();
		jsonType.put("attrDefs", jsonAttrdefs);
		
		for ( int i = 0; i < attrdefs.size(); i++ ) {
			Attrdef attrdef = attrdefs.get(i);
			
			ObjectNode  jsonAttrdef  = mapper.createObjectNode();
			jsonAttrdefs.add(jsonAttrdef);
			
			if (attrdef.type == Xattr.Type.TPE) {
				jsonAttrdef.put("type", "TPE");
				jsonAttrdef.put("name", attrdef.name);
			}
			else if (attrdef.type == Xattr.Type.GRP) {
				jsonAttrdef.put("type", "GRP");
				jsonAttrdef.put("name", attrdef.name);
			}
			else
			{	jsonAttrdef.put("type",     "STR");
				jsonAttrdef.put("name",     attrdef.name);
				jsonAttrdef.put("minCount", attrdef.minCount);
				jsonAttrdef.put("maxCount", attrdef.maxCount);
				jsonAttrdef.put("value",    attrdef.value);
			}
		}	
		
		ArrayNode jsonDescrs = mapper.createArrayNode();
		jsonType.put("descrs", jsonDescrs);
		
		for ( int i = 0; i < descrs.size(); i++ ) {
			Descr descr = descrs.get(i);
			
			ObjectNode  jsonDescr  = mapper.createObjectNode();
			jsonDescrs.add(jsonDescr);
			
			ArrayNode jsonItems = mapper.createArrayNode();
			jsonDescr.put("items", jsonItems);
			
			for (int ii = 0; ii < descr.items.size(); ii++) {
				DescrItem descritem = descr.items.get(ii);
				
				ObjectNode  jsonItem  = mapper.createObjectNode();
				jsonItems.add(jsonItem);
				
				if (descritem instanceof DescrItemValue) {
					DescrItemValue itValue = (DescrItemValue)descritem;
					jsonItem.put("type", "VALUE");
					jsonItem.put("value", itValue.value);
					
				}
				else
				{	DescrItemPath itPath = (DescrItemPath)descritem;
					jsonItem.put("type", "PATH");
					
					ArrayNode jsonPath = mapper.createArrayNode();
					jsonItem.put("path", jsonPath);
					
					for (int pi = 0; pi < itPath.path.size(); pi++) {
						ObjectNode  jsonPathItem  = mapper.createObjectNode();
						jsonPath.add(jsonPathItem);
						
						jsonPathItem.put("value", itPath.path.get(pi));
					}
				}
			}
		}

	}
}
